package com.lxm.service.impl;

import com.lxm.entity.Category;
import com.lxm.dao.CategoryMapper;
import com.lxm.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author lxm
 * @since 2021-11-23
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {


    //根据id查子类别，只查一层的
    public List<Category> selectChildren(int categoryId) {
        return baseMapper.selectChildren(categoryId);
    }

    @Override
    //根据id类别，如果有子类别，则提示不能删除
    public Boolean deleteCate(int categoryId) {
        List<Category> categories = baseMapper.selectChildren(categoryId);
        if(categories.size()==0){
            int i = baseMapper.deleteById(categoryId);
            return true;
        }
        return false;
    }
    //查完整的权限树
    public List<Category> selectTree(){
        //这个是全部的类别
        List<Category> categories = baseMapper.selectList(null);
        List<Category> list = categories.stream().filter(cate -> cate.getParentId() == 0).peek(cate -> cate.setCategoryList(selectAllChildren(cate.getCategoryId(), categories))).collect(Collectors.toList());
        return list;
    }
    public List<Category>selectAllChildren(int pId,List<Category> list){
        List<Category> collect = list.stream().filter(category -> category.getParentId() == pId).peek(category -> category.setCategoryList(selectAllChildren(category.getCategoryId(), list))).collect(Collectors.toList());
        return collect;
    }

}
